package com.vitas.coin.vm

import android.Manifest
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.RecyclerView
import com.snap.base.util.BaseUtil
import com.snap.util.startAct
import com.vitas.coin.R
import com.vitas.coin.data.bean.LocalMusicInfoBean
import com.vitas.coin.data.event.AddSuccessEvent
import com.vitas.coin.data.event.RemoveEvent
import com.vitas.coin.resp.UploadResp
import com.vitas.coin.ui.act.ChooseLocalMusicListAct
import com.vitas.coin.util.Http
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.Collections

class MakeVM : BaseMusicVM() {


    var parentId: Long = 1L;

    val uploadResp = UploadResp()

    var musicList: MutableList<LocalMusicInfoBean> = ArrayList()


    fun getUrl(url: String) {
        GlobalScope.launch {
            var param = HashMap<String, String>()
            param.put("url", url)
            try {
                var str = Http.get(
                    "https://jk.y-l.cc/api/dsp/3240EB6120B97E82CEDAA757A6D8CC1A17C46F076ED9714079/202037242/",
                    param, Collections.emptyMap<String, String>()
                )

                Log.e("tagg", "get:$str")
            } catch (e: Throwable) {
                e.printStackTrace()
            }
        }
    }

    fun initParentId(parentId: Long) {
        this.parentId = parentId
    }


    fun addMusic(musicBean: LocalMusicInfoBean) {
        musicList.add(musicBean)
        musicAdapter.notifyDataSetChanged()
        BaseUtil.postEvent(AddSuccessEvent())
    }


    fun removeMusic(position: Int) {
        musicList.removeAt(position)
        musicAdapter.notifyItemRemoved(position)
        BaseUtil.postEvent(RemoveEvent())
    }


    fun onClickAddPhoto() {
        val requestList = ArrayList<String>()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            requestList.add(Manifest.permission.READ_MEDIA_AUDIO)
            requestList.add(Manifest.permission.READ_MEDIA_VIDEO)
        } else {
            requestList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        }
        BaseUtil.requestPermission(
            actionAct.invoke(),
            requestList,
            "上传本地音乐需要使用存储权限，请您授予该权限"
        ) {
            if (!it) {
                return@requestPermission
            }
            startAct(ChooseLocalMusicListAct::class.java)
        }
    }


    val musicAdapter by lazy { MusicUploadAdapter() }


    inner class MusicUploadAdapter : RecyclerView.Adapter<MusicUploadAdapter.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view =
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_music_upload, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            musicList.get(position).apply {
                holder.tv_value.text =
                    musicList.get(position).songName + " " + musicList.get(position).formatDuration
                holder.iv_cancel.setOnClickListener { v ->
                    removeMusic(position)
                }
            }
        }

        override fun getItemCount(): Int {
            return musicList.size
        }

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val tv_value: TextView = itemView.findViewById(R.id.tv_value)
            val iv_cancel: AppCompatImageView = itemView.findViewById(R.id.iv_cancel)
        }

    }


}